* Reset settings and initialize log file
launch, path("share/synchrony")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Show that the timing of the summer drop in female employment
*              aligns with cross-state differences in school-year timing.
*-------------------------------------------------------------------------------


* Run specifications
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Load data on adult individuals
	gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear
	keep pid tm year month wtfinl wtraked tmspline* weeks female state_fips emp school

	* Merge in state-level data on the typical timing of school closures
	merge m:1 state_fips using "$basepath/data/derived/state_closure_timing.dta", assert(3) nogenerate keepusing(closure_timing)

	* Create one sample pooling across states, another splitting by state closure timing
	gisid pid tm
	expand = 2
	bysort pid tm: replace closure_timing = 0 if _n == 2

	* Store the data
	tempfile core
	save `core'

	* Compute the hazard rate of separation among those employed outside education
	gen byte sep = (L.emp == 1 & emp == 0) if !missing(wtraked)
	keep if L.emp == 1 & L.school == 0
	gcollapse (mean) sep (rawsum) wtraked [pw = wtraked], by(female closure_timing tm)
	tempfile flows
	save `flows'

	* Compute aggregate stocks and weights
	use `core', clear
	gcollapse (mean) emp (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(female closure_timing tm)

	* Combine stocks and flows
	merge 1:1 female closure_timing tm using `flows', assert(1 3) nogenerate
	tempfile core
	save `core'

	* Loop over sex
	foreach f of numlist 0 1 {
		* Loop over state classifications
		foreach s of numlist 0 1 2 3 {
			* Reload sample
			use if female == `f' & closure_timing == `s' using `core', clear
			quietly tsset tm

			* Employment
			quietly ivreg2 emp ib5.month tmspline* weeks [aw = wtfinl], bw($bandwidth) robust small
			process_estimates, path("synchrony") model("f`f'_emp_states`s'")

			* Separations from non-educational employment
			quietly ivreg2 sep ib5.month D.tmspline* D.weeks [aw = wtraked], bw($bandwidth) robust small
			process_estimates, path("synchrony") model("f`f'_sep_states`s'")
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("synchrony")

* Prepare coefficient labels for each month
make_coeflabels


* Plot school enrollment (left) and male/female EPOP (right) by closure timing
*-------------------------------------------------------------------------------

* Compute the monthly fraction of 16-year-olds in school in each group of states
gzuse "$basepath/data/derived/cps_bms_sample_allages.dta.gz", clear
keep if age == 16
keep pid tm month wtfinl age school_status state_fips
merge m:1 state_fips using "$basepath/data/derived/state_closure_timing.dta", assert(3) nogenerate
gen byte share_enrolled = 100 * (school_status == 1)
gcollapse (mean) share_enrolled [pw = wtfinl], by(closure_timing month)

* Adjust x-values for visual clarity
gen month_adj = month - 5
replace month_adj = month_adj + 12 if month_adj < 0
replace month_adj = month_adj - .05 if closure_timing == 1
replace month_adj = month_adj + .00 if closure_timing == 2
replace month_adj = month_adj + .05 if closure_timing == 3
sort closure_timing month_adj

* Prepare shading
gen rlow = 0
gen rupp = 100
gen rval = month_adj - 0.5

* Store data
tempfile core
save `core'

* Loop over sex
foreach f of numlist 0 1 {
	if `f' == 0 {
		local flbl "men"
		local fadj "Male"
		local col1 "color($col1)"
		local col2 "color($dkgs)"
		local col3 "color($col2)"
		local ci1 "ciopts(color($col1))"
		local ci2 "ciopts(color($dkgs))"
		local ci3 "ciopts(color($col2))"
	}
	else if `f' == 1 {
		local flbl "women"
		local fadj "Female"
		local col1 "color(black)"
		local col2 "color(gs8)"
		local col3 "color(gs8) mfcolor(white)"
		local ci1 "ciopts(color(black) lwidth(vthin))"
		local ci2 "ciopts(color(gs8) lwidth(vthin))"
		local ci3 "ciopts(color(gs8) fcolor(white) lwidth(vthin))"
	}

	* Reload data
	use `core', clear

	* Left panel: share of 16-year-olds enrolled in high school in early/late states
	#delimit ;
	twoway
		(rarea rupp rlow rval if inrange(month, 5, 10), color($ltgs) plotregion(margin(t=0 b=0)))
		(connected share_enrolled month_adj if closure_timing == 1, `col1' msymbol($sym1))
		(connected share_enrolled month_adj if closure_timing == 2, `col2' msymbol($sym2))
		(connected share_enrolled month_adj if closure_timing == 3, `col3' msymbol($sym3)),
		title("{bf:High school enrollment}")
		xtitle("")
		xscale(range(-0.5 12.5))
		xlabel(
			0 "May" 1 "June" 2 "July" 3 "Aug." 4 "Sept." 5 "Oct."
			6 "Nov." 7 "Dec." 8 "Jan." 9 "Feb." 10 "Mar." 11 "Apr."
			12 "May", alternate)
		xtick(1(1)12)
		ytitle("Percent of 16-year-olds")
		yscale(range(0 100))
		ylabel(0(20)100)
		ytick(0(10)100)
		plotregion(margin(l=0))
		legend(rows(1) size(*.8) order(2 3 4)
			label(2 "Early-closure states")
			label(3 "Mixed-closure states")
			label(4 "Late-closure states"));
	#delimit cr

	tempfile left
	graph save "`left'"

	* Right panel: male/female EPOP in early/late states
	clear
	set obs 14
	gen rlow = -2
	gen rupp = 1
	gen rval = _n - 0.5

	#delimit ;
	coefplot
		(f`f'_emp_states1, offset(-.05) recast(connected) `col1' `ci1' label("Early-closure states"))
		(f`f'_emp_states2, offset(0.00) recast(connected) `col2' `ci2' label("Mixed-closure states"))
		(f`f'_emp_states3, offset(+.05) recast(connected) `col3' `ci3' label("Late-closure states")),
		coeflabels(`coeflabels')
		title("{bf:`fadj' employment}")
		xtitle("")
		xlabel(, `notick' alternate)
		ytitle("Difference relative to May (p.p.)")
		yscale(range(-2 1.0))
		ylabel(-2(0.5)1.0, format(%5.1f))
		yline(0)
		addplot(
			rarea rupp rlow rval if inrange(rval, 0.5, 5.5), color($ltgs) plotregion(margin(t=0 b=0)) below ||
			scatteri 0 0.5 0 13.5, recast(line) color(black) below)
		vertical
		legend(rows(1) size(*.8))
		rescale(100);
	#delimit cr

	tempfile right
	graph save "`right'"

	* Combine panels: paper version
	grc1leg "`left'" "`right'"
	nicepdf "$basepath/output/synchrony_epop_`flbl'.pdf", panels(2) indirect replace
}


* Plot exit hazards from non-ed, overall (left) and in early/late states (right)
*-------------------------------------------------------------------------------

* Left panel: hazard rate of separation among female vs. male non-ed workers
clear
set obs 14
gen rlow = -.5
gen rupp = 1.5
gen rval = _n - 0.5

#delimit ;
coefplot
	(f1_sep_states0, offset(-.05) recast(connected) color(black) ciopts(color(black)) label("Women"))
	(f0_sep_states0, offset(+.05) recast(connected) color(gs6) ciopts(color(gs6)) mfcolor(white) label("Men")),
	coeflabels(`coeflabels')
	title("{bf:All workers not in the education sector}")
	xtitle("")
	xlabel(, alternate)
	ytitle("")
	yscale(range(-.5 1.5))
	ylabel(-.5(.5)1.5, format(%5.1f))
	addplot(
		rarea rupp rlow rval if inrange(rval, 0.5, 5.5), color($ltgs) plotregion(margin(t=0 b=0)) below ||
		scatteri 0 0.5 0 13.5, recast(line) color(black) below)
	vertical
	legend(rows(1) size(*.8))
	rescale(100);
#delimit cr

tempfile left
graph save "`left'"

* Right panel: hazard rate of separation by sex in early/late states
clear
set obs 14
gen rlow = -.6
gen rupp = 1.6
gen rval = _n - 0.5

foreach s of numlist 1 3 {
	if `s' == 1 local slbl "Early-closure states"
	if `s' == 3 local slbl "Late-closure states"

	#delimit ;
	coefplot
		(f1_sep_states`s', offset(-.05) recast(connected) color(black) ciopts(color(black)) label("Women"))
		(f0_sep_states`s', offset(+.05) recast(connected) color(gs6) ciopts(color(gs6)) mfcolor(white) label("Men")),
		coeflabels(coef0 = "May" coef1 = "J" coef2 = "J" coef3 = "A" coef4 = "S" coef5 = "O" coef6 = "N" coef7 = "D" coef8 = "J" coef9 = "F" coef10 = "M" coef11 = "A" coef12 = "May")
		title("{bf:`slbl'}")
		xtitle("")
		ytitle("")
		yscale(range(-.6 1.6))
		ylabel(-.5(.5)1.5, format(%5.1f))
		yline(0)
		addplot(
			rarea rupp rlow rval if inrange(rval, 0.5, 5.5), color($ltgs) plotregion(margin(t=0 b=0)) below ||
			scatteri 0 0.5 0 13.5, recast(line) color(black) below)
		vertical
		graphregion(margin(vsmall))
		plotregion(margin(t=0 b=0 l=1 r=1))
		legend(rows(1))
		rescale(100);
	#delimit cr

	tempfile g_s`s'
	graph save "`g_s`s''"
}

* Combine panels
grc1leg "`g_s1'" "`g_s3'", iscale(*1.2) rows(2)
tempfile right
graph save "`right'"

* Export figure
grc1leg "`left'" "`right'", rows(1) l1title("      Difference relative to May (p.p.)", size(*.8)) imargin(vsmall)
nicepdf "$basepath/output/synchrony_noned_sephaz.pdf", panels(2) indirect replace

* Close the log file
unlaunch
